#include <bits/stdc++.h>

//#pragma GCC optimize(2)
#define For(x, y, z) for (int x = y; x <= z; ++x)
#define Rof(x, y, z) for (int x = y; x >= z; --x)
#define Eor(u) for (int i = head[u]; i; i = nxt[i])
#define SZ(x) (int(x.size()))
#define pb push_back

using namespace std;
using i64 = long long;
using ull = unsigned long long;
using pii = pair<int, int>;

char buf[(1<<21)+5],*p1,*p2;
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)

inline int read() {
	int x = 0, f = 0; char ch = getchar();
	while (!isdigit(ch)) f |= (ch == '-'), ch = getchar();
	while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
	return f ? -x : x;
}
int __stk[128], __tp;
inline void put(i64 x) {
	if (x < 0) putchar('-'), x = -x;
	do { __stk[++__tp] = x % 10, x /= 10; } while (x);
	while (__tp) putchar(__stk[__tp--] + '0');
}

const int mod = 147744151;
inline int ksm(int x, int y, int res = 1) {
	for ( ; y; y >>= 1, x = 1ll * x * x % mod)
		(y & 1) && (res = 1ll * res * x % mod);
	return res;
}
inline int inv(int x) { return ksm(x, mod - 2); }
inline int gcd(int a, int b) { if (b) while ((a %= b) && (b %= a)); return a | b; }
inline void add(int &x, int y) { (x += y) >= mod && (x -= mod); }
inline void Min(int &x, int y) { (y < x) && (x = y); }
inline void Max(int &x, int y) { (y > x) && (x = y); }

char qqq;

const int N = 1024;
int n, m, p, lg[N];
int a[N][N][11][11], ans;

char qqqq;
signed main() {
	freopen("tsuki.in", "r", stdin);
	freopen("tsuki.out", "w", stdout);

    cerr << (&qqq-&qqqq) / 1024.0 / 1024.0 << '\n';

    n = read(), m = read(), p = read();
    lg[0] = -1; For(i, 1, max(n, m)) lg[i] = lg[i >> 1] + 1;
    For(i, 1, n) For(j, 1, m) a[i][j][0][0] = read();
    For(i, 1, n) Rof(j, m, 1) For(y, 1, lg[m - j + 1]) 
        a[i][j][0][y] = 1ll * a[i][j][0][y - 1] * a[i][j + (1 << (y - 1))][0][y - 1] % p;
    
    Rof(i, n, 1) For(j, 1, m) For(x, 1, lg[n - i + 1]) For(y, 0, lg[m - j + 1])
        a[i][j][x][y] = 1ll * a[i][j][x - 1][y] * a[i + (1 << (x - 1))][j][x - 1][y] % p;

    int Q = read(); For(_, 1, Q) { 
        int res = 1;
        int sx = read(), sy = read();
        int ex = read(), ey = read();

        while (sx <= ex) { 
            int o = sy, l1 = lg[ex - sx + 1];
            while (o <= ey) { 
                int l2 = lg[ey - o + 1];
                res = 1ll * res * a[sx][o][l1][l2] % p;
                o += 1 << l2;
            } sx += 1 << l1;
        }  add(ans, (_ ^ res) % mod);
    } 
    cout << ans;
    cerr << clock() / 1000.0 <<'\n';
	return 0;
}